﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for VideoProvider
/// </summary>
namespace EHTWebsite.Code
{
    public class VideoProvider
    {
        /// <summary>
        /// lay tat ca video duoc pulish
        /// </summary>
        /// <param name="isAdd"></param>
        /// <returns></returns>
        public static DataSet GetVideos(Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetVideos";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@IsSelect", isAdd));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }

        /// <summary>
        /// lay video theo ID
        /// </summary>
        /// <param name="VideoID"></param>
        /// <returns></returns>
        public static Video GetVideoByVideoID(Int32 VideoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetVideoByVideoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VideoID", VideoID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            Video theVideo = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theVideo = new Video();
                theVideo.VideoID = Int32.Parse(ds.Tables[0].Rows[0]["VideoID"].ToString());
                theVideo.VideoUrl = ds.Tables[0].Rows[0]["VideoUrl"].ToString();
                theVideo.MoTaVN = ds.Tables[0].Rows[0]["MoTaVN"].ToString();
                theVideo.MoTaOther = ds.Tables[0].Rows[0]["MoTaOther"].ToString();               
            }

            return theVideo;
        }

        /// <summary>
        /// Insert video vao database
        /// </summary>
        /// <param name="theVideo"></param>
        /// <param name="isAdd"></param>
        /// <returns></returns>
        public static Int32 IUVideos(Video theVideo, Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUVideos";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theVideoParameter = new SqlParameter();

            theVideoParameter.ParameterName = "@VideoIDOut";
            theVideoParameter.Direction = ParameterDirection.Output;
            theVideoParameter.DbType = DbType.Int32;

            command.Parameters.Add(theVideoParameter);

            command.Parameters.Add(new SqlParameter("@VideoID", theVideo.VideoID));
            command.Parameters.Add(new SqlParameter("@VideoUrl", theVideo.VideoUrl));
            command.Parameters.Add(new SqlParameter("@MoTaVN", theVideo.MoTaVN));
            command.Parameters.Add(new SqlParameter("@MoTaOther", theVideo.MoTaOther));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theVideo.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));
            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@VideoIDOut"].Value.ToString());
        }

        /// <summary>
        /// Xoa video theo ID
        /// </summary>
        /// <param name="VideoID"></param>
        public static void DeleteVideoByVideoID(Int32 VideoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteVideoByVideoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VideoID", VideoID));
            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Lay video co phan trang
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static DataSet GetVideosWithPaging(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetVideosWithPaging";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        /// <summary>
        /// thay doi trang thai cua video
        /// </summary>
        /// <param name="VideoID"></param>
        /// <param name="publish"></param>
        public static void ChangePublish(Int32 VideoID, Boolean publish)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VideoID", VideoID));
            command.Parameters.Add(new SqlParameter("@Publish", publish));
            DataProvider.ExecuteNoneQuery(command);
        }

    }

}
